//
//  BZTabBar.swift
//  weibo
//
//  Created by zzx on 2017/7/22.
//  Copyright © 2017年 zzx. All rights reserved.
//

import UIKit

//设置代理
protocol BZTabBarDelegate: NSObjectProtocol{
    
    func clickComposeBottonOperation()

}







class BZTabBar: UITabBar {

    
    ///按钮执行闭包
    var composeButtonClouse: (() -> ())?
    
    //定义代理
    weak var bzTabBarDelegate:BZTabBarDelegate?
    
    //懒加载
    private lazy var composeButton: UIButton = {
        
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: .normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: .highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), for: .normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        
        button.sizeToFit()
        
        //添加点击事件
        button.addTarget(self, action: #selector(composeButtonAction), for: .touchUpInside)
        
        
        
        return button
        
    }()
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    
    }
    
    
    func setupUI() {
        
        //消除跳转时的阴影
        backgroundImage = UIImage(named: "tabbar_background")
        
        addSubview(composeButton)
        
        
    }
    
    //MARK:- 设置布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        composeButton.center.x = width / 2
        
        composeButton.center.y = height / 2
        
        //计算宽度
        let buttonWidth = width / 5
        
        //下标
        var index = 0
        
        //循环遍历
        for subView in self.subviews {
            
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                subView.width = buttonWidth
                
                subView.x = CGFloat(index) * buttonWidth
                
                index += 1
                
                if index == 2 {
                    
                    index += 1
                    
                }
            }
            
        }
        
    }
    
    
    //MARK:- 按钮方法的实现
    func composeButtonAction() {
        
        print("asdff")
        
        composeButtonClouse?()
        
        bzTabBarDelegate?.clickComposeBottonOperation()
    }
    
    
    
    

}
